package syerit.core.ui
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	
	import syerit.core.interfaces.IDisposable;
	
	public class ColorLabel extends Sprite implements IDisposable
	{
		public var image:Bitmap;
		public var imageSource:DisplayObject;
		private var trigger:Function;
		private var triggerParams:Array;
		private var color:uint;
		private var newColor:ColorTransform;
		
		public function ColorLabel(imageSource:Bitmap,x:Number=0,y:Number=0,color:uint=0x000000,trigger:Function=null,triggerParams:Array=null,index:int=0)
		{
			super();
			
			this.x=x;
			this.y=y;
			this.imageSource=imageSource;
			this.trigger= trigger;
			this.triggerParams= triggerParams;
			this.color = color;
			
			
			register();
		}
		
		public function register():void
		{
			var bmdOut:BitmapData = new BitmapData(imageSource.width,imageSource.height,true,0x000000);
			bmdOut.draw(imageSource);
			image = new Bitmap(bmdOut);
			
			newColor  =  this.transform.colorTransform;
			newColor.color = color;
			this.transform.colorTransform = newColor;
			
			activate();
		}
		
		public function activate():void
		{
			addChild(image);
			if(trigger!=null)
			{
				addEventListener(MouseEvent.CLICK,mouseClickHandler);
			}
		}
		
		public function deactivate():void
		{
			removeEventListener(MouseEvent.CLICK,mouseClickHandler);
		}
		
		public function dispose():void
		{
			deactivate();
		}
		
		private function mouseClickHandler(e:MouseEvent):void
		{
			trigger.apply(this,triggerParams);
		}
		
	}
}